/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_HTTP_SERVER_H
#define MK_HTTP_SERVER_H
#include "MkTcp/MkTcpServer.h"
#include "MkHttpUtil.h"
#include "MkUtil/MkQueue.h"

class CMkHttpRequest;
class CMkHttpSession;
class MkApi CMkHttpServer :public CMkTcpServer
{
public:
    CMkHttpServer(CMkEventPool* pEventPool, CMkTimer* pTimer);
    virtual ~CMkHttpServer();

    void SetKeepAliveSeconds(Uint32 Seconds) { m_KeepAliveSeconds = Seconds; }
    void SetMaxKeepAliveRequest(Uint32 ReqCount) { m_MaxKeepAliveRequests = ReqCount; }

    Uint32 GetKeepAliveSeconds() const { return m_KeepAliveSeconds; }
    Uint32 GetMaxKeepAliveRequest() const { return m_MaxKeepAliveRequests; }

    Uint32 SendResponse(const Uint64& HttpSessionId, const MkHttpStatusCode& Error, const MkHttpBodyType& BodyType = MkHttpBodyTypeNone, const MkString& Body = "");
    Uint32 SendDownloadResponse(const Uint64& HttpSession, FILE* pFile, Uint64 FileSize);
    Uint32 SendWebSocketText(const Uint64& HttpSessionId, const MkString& Msg);
    Uint32 SendWebSocketBinary(const Uint64& HttpSessionId, const CMkBufferList& BufList, BOOL bMark = FALSE);
    Uint32 SendWebSocketClose(const Uint64& WsSessionId);
private:
    virtual Uint32 OnRecvRequest(CMkHttpSession* pSession, const CMkHttpRequest& RequestHeader) = 0;
    virtual Uint32 OnWebsocketRecved(CMkHttpSession* pSession, const MkString& Message) = 0;

    virtual Uint32 OnWebsocketClosed(CMkHttpSession* pSession) = 0;
    friend class CMkHttpSession;
protected:
    virtual CMkTcpSession* CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer) override;
    virtual Uint32 OnSessionEventRemoved(CMkTcpSession* pSession) override;
private:
    Uint32                                      m_KeepAliveSeconds;
    Uint32                                      m_MaxKeepAliveRequests;
};
extern const  MkString NotFoundString;
#endif